# -*- encoding: utf-8 -*-

# Copyright 2010 István Szentandrási
# <etude-music-player@googlegroups.com>

# This file is part of Etude.
#
# Etude is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as 
# published by the Free Software Foundation, either version 3 
# of the License, or (at your option) any later version.
#
# Etude is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Etude. If not, see <http://www.gnu.org/licenses/>.

from . import _

import os

HAVE_VIS = True
import xmmsclient
import xmmsclient.glib
if not hasattr(xmmsclient.XMMS, 'visualization_version'):
    HAVE_VIS = False
    import xmms_vis

class VisWrapper():

    """
    Wrapper around xmms2 visualization functions
    """

    def __init__(self, plugin):
        self.control = plugin.control
        self.plugin = plugin
        self.visualization = False
        if HAVE_VIS:
            self.xmms = self.control.client.xmms_slow
        else:
            print("Creating new connection")
            self.xmms = xmms_vis.XMMS("Etude-visualization")
            url = self.control.profile['url']
            if url == '':
                url = os.getenv('XMMS_PATH')
            print("Connecting to %s" % url)
            self.xmms.connect(url, self.quit)
            self.glibc = xmmsclient.glib.GLibConnector(self.xmms)
    
    def init_visualization(self, dict):
        res = self.xmms.visualization_version()
        res.wait()
        if res.iserror():
            print("Visualization init failed")
            self.quit()
            return
        else:
            version = res.value()
            if version < 1:
                print("Visualization too old.")
                self.quit()
                return
        x = self.xmms
        res = self.xmms.visualization_init()
        res.wait()
        if res.iserror():
            print("Visualization init failed")
            print(res.value())
            self.quit()
            return

        vis = x.visualization_init_handle(res)
        if vis <0:
            print("Something went wrong during initialization.")
            self.quit()
            return

        res = x.visualization_properties_set(vis, dict)
        res.wait()
        if(res.iserror()):
            print("Visualization init failed")
            print(res.value())
            self.quit()
            return
        
        counter = 0
        while not x.visualization_started(vis) or counter == 3:
            res = x.visualization_start(vis)
            if x.visualization_errored(vis):
                print("Couldn't start visualization: %s" % x.get_last_error())
                self.quit()
                return
            res.wait()
            x.visualization_start_handle(res)
            print(counter)
            counter += 1
        if counter == 3:
            self.visualization = False
            self.quit()
            return

        self.vis = vis
        self.visualization = True
        print("visualization successful %i" % self.vis)

    def stop_visualization(self):
        self.xmms.visualization_shutdown(self.vis)

    def chunk_get(self, drawtime, blocking, buffer_size = 1024):
        return self.xmms.visualization_chunk_get(self.vis, drawtime, blocking, buffer_size)


    def quit(self, xmms):
        print("Quitting visualization wrapper")
        if not HAVE_VIS:
            try:
                del self.xmms
                del self.glibc
            except:
                pass
        self.plugin.unload()

